package client;

import java.io.*;
import java.net.*;


public class Server{
	public static void main(String[] args){
		listen l = new listen(111);
		l.rejestracja();
	}
}
class listen{
	int port;
	listen(int p){
		port = p;
	}
	void rejestracja(){
		try{
			ServerSocket ss = new ServerSocket(1025);		
			Socket s;
			while(true){
				System.out.println("Czekam na chetnych do rejestracji");
				s = ss.accept();	
                                InetAddress ia = s.getLocalAddress();
                                System.out.println("Wiadomosc od: ");
				/*adres IP przeklejam do bazy*/

				ObjectInputStream in =new ObjectInputStream(new BufferedInputStream(s.getInputStream()));
				Object ob = in.readObject();
				if(ob == null) {
   					System.out.println("Null");
				}
				else if (ob instanceof msg) {
   					msg m = (msg)ob;
   					m.display();
				}
				else{
   					throw new ClassNotFoundException("oczekiwano rejestracji - obiekt klasy "+ob);
				}

				/*tu bedzie zapis pobranych danych do bazy*/
				/*zwaracam id użytkownika który dostane z bazy*/
				DataOutputStream out = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
				out.writeInt(12345);
				out.flush();
		    	        s.close();
			}
		}
		catch(IOException e){
			System.err.println(e);
		}
		catch(ClassNotFoundException e) { System.out.println(e); }
	}
}


class msg implements java.io.Serializable{
        // static final long serialVersionUID
    
        String name;
        int age;
        String passwd_hash;
        String email;
        msg(String n, int a, String p, String e){
                name = n;
                age = a;
                passwd_hash = p;
                email = e;
        }
	void display(){
		System.out.println("Imie: "+name+"\nWiek: "+age+"\nPasswd: "+passwd_hash+"\nEmail: "+email);
	}
}

